package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.szgr.baoyin.MainActivity;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.CommonImagePagerAdapter;
import com.szgr.baoyin.adapter.FirstInstallPagerAdapter;
import com.szgr.baoyin.model.HomePageImage;
import com.szgr.baoyin.utils.MyTool;

import java.util.ArrayList;
import java.util.List;

import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

public class FirstInstallActivity extends Activity implements View.OnTouchListener, View.OnClickListener {
    private int[] data;
    private AutoScrollViewPager viewPager;
    private LinearLayout indicator;
    private float oldX;
    private float oldY;
    private float newX;
    private float newY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_install);

        initView();
    }

    private void initView() {
        data = new int[4];
        data[0] = R.drawable.index_01;
        data[1] = R.drawable.index_02;
        data[2] = R.drawable.index_03;
        data[3] = R.drawable.index_04;

        viewPager = (AutoScrollViewPager) findViewById(R.id.first_install_pager);
        indicator = (LinearLayout) findViewById(R.id.first_install_indicator);
        viewPager.setOnTouchListener(this);
//        findViewById(R.id.root).setOnClickListener(this);
        show(data);
    }

    private void show(int[] list) {
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(new FirstInstallPagerAdapter(this, list));
        viewPager.setInterval(5000);
        viewPager.setScrollDurationFactor(3);
//        if (list.size() > 1)
//            viewPager.startAutoScroll();

//        bannserSize = list.size();
//        bannerIsScrolling = true;
        final List<ImageView> textViewList = initIndicator(list);
//        viewPager.setCurrentItem(Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2) % textViewList.size());
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            int index = 0;

            @Override
            public void onPageSelected(int arg0) {
                index = arg0 % textViewList.size();
                for (int i = 0; i < textViewList.size(); i++) {
                    textViewList.get(i).setBackgroundResource(R.drawable.judge_01);
                }
                textViewList.get(index).setBackgroundResource(R.drawable.judge_02);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    private List<ImageView> initIndicator(int[] list) {
        List<ImageView> textViewList = new ArrayList<ImageView>();
        ImageView textView;
        for (int i = 0; i < list.length; i++) {
            textView = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MyTool.dip2px(7), MyTool.dip2px(7));
            params.setMargins(MyTool.dip2px(10),0,0,0);
            textView.setLayoutParams(params);

            if (0 == i) {
                textView.setBackgroundResource(R.drawable.judge_02);
            } else {
                textView.setBackgroundResource(R.drawable.judge_01);
            }
            indicator.addView(textView);
            textViewList.add(textView);
        }
        return textViewList;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                oldX = event.getX();
                oldY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                newX = event.getX();
                newY = event.getY();

                if(oldX - newX > 30 && viewPager.getCurrentItem() == 3){
                    startActivity(new Intent(this, MainActivity.class));
                    MyTool.saveSP("isFirstInstall","YES");
                    finish();
                }
                break;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if(viewPager.getCurrentItem() == 3){
            startActivity(new Intent(this, MainActivity.class));
            MyTool.saveSP("isFirstInstall","YES");
            finish();
        }
    }
}
